package com.example.base

import android.app.Application
import com.alibaba.android.arouter.launcher.ARouter
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.util.concurrent.Executors
import kotlin.coroutines.CoroutineContext

//基础优化APP

open class BaseApplication:Application() {
    //APP应用启动分为//
    //冷启动:之前的APP没有启动过,也就是APPlication中的onCreate()方法没有执行
    //热启动:之前APP启动过,在后台近期应用列表中可以找到
    //温启动:之前APP启动过,ACTIVITY重新创建,执行ACTIVITY的ONCREATE()方法
    companion object {
        lateinit var appContext: BaseApplication
    }

    override fun onCreate() {
        super.onCreate()
        appContext = this

        //MMKV初始化
        asyncInit(appContext)




    }

    private fun asyncInit(application: Application) {
        runBlocking {
            //穿件协程作用域对象
            var dispatcher = Executors.newFixedThreadPool(1).asCoroutineDispatcher()
            launch(dispatcher) {
                try {
                    MMKV.initialize(appContext)

                    if (BuildConfig.DEBUG){
                        //Arouter
                        ARouter.openLog()
                        ARouter.openDebug()
                    }
                    ARouter.init(appContext)
                }catch (e:Exception){
                    e.printStackTrace()
                }finally {
                    cancel()
                }
            }
        }
    }

}